import java.util.*;
class Solution {

    static class TreeNode {
        public int val;//存数据
        public TreeNode left;//左孩子的引用
        public TreeNode right;//右孩子的引用

        public TreeNode(int val) {
            this.val = val;
        }
    }

    // 创建一棵二叉树 返回这棵树的根节点
    public TreeNode createBinaryTree() {
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        node1.left = node2;
//        TreeNode node1 = new TreeNode(5);
//        TreeNode node2 = new TreeNode(4);
//        TreeNode node3 = new TreeNode(8);
//        TreeNode node4 = new TreeNode(11);
//        TreeNode node5 = new TreeNode(13);
//        TreeNode node6 = new TreeNode(4);
//        TreeNode node7 = new TreeNode(7);
//        TreeNode node8 = new TreeNode(2);
//        TreeNode node9 = new TreeNode(1);
//        TreeNode root = node1;
//        node1.left = node2;
//        node1.right = node3;
//        node2.left = node4;
//        node4.left = node7;
//        node4.right = node8;
//        node3.left = node5;
//        node3.right = node6;
//        node6.right = node1;
        return node1;
    }
    public boolean hasPathSum(TreeNode root, int sum) {
        if (root == null) {
            return false;
        }
        if (root.left == null && root.right == null) {
            return sum == root.val;
        }
        return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
    }
    /*public int sum = 0;
    public boolean hasPathSum1(TreeNode root, int targetSum) {
        if(root ==null)return false;
        sum = sum + root.val;
        if(hasPathsum(root.left,targetSum)==targetSum)return true;
        sum = root.val;
        if(hasPathsum(root.right,targetSum)==targetSum)return true;
        return false;
    }
    public int hasPathsum(TreeNode root, int targetSum) {
        if(root == null)return 0;
        sum = sum + root.val;
        if(sum == targetSum)return sum;
        if(sum > targetSum)return sum - root.val;
        sum = hasPathsum(root.left,targetSum);
        if(sum == targetSum)return sum;
        if(sum > targetSum)return sum - root.val;
        sum = hasPathsum(root.right,targetSum);
        return sum;
    }*/

    int ans;
    int rootvalue;

    public int findSecondMinimumValue(TreeNode root) {
        ans = -1;
        rootvalue = root.val;
        dfs(root);
        return ans;
    }

    public void dfs(TreeNode node) {
        if (node == null) {
            return;
        }
        if (ans != -1 && node.val >= ans) {
            return;
        }
        if (node.val > rootvalue) {
            ans = node.val;
        }
        dfs(node.left);
        dfs(node.right);
    }
}